<?php
/**
 ***********************************************************************************************************************
 * 客户管理V2版本
 ***********************************************************************************************************************
 */
namespace soa\account;
use soa\client;

/**
 * Class crm
 * @package soa\account
 */
class crm extends client
{
    /**
     * 服务地址
     *
     * @var string
     */
    public $service_url = 'http://account.core.kuaidihelp.com/';

    /**
     * 服务名称
     *
     * @var string
     */
    protected $service_name = 'kuaidihelp_account';

    /**
     * 超时时间设置
     *
     * @var int
     */
    protected $_timeout = 10;

    /**
     * @var int
     */
    protected $courier_id = 0;

    /**
     * @param $courier_id
     */
    public function __construct($courier_id)
    {
        $this->courier_id = $courier_id;
    }

    /**
     * @return int
     */
    public function getCourierID()
    {
        return $this->courier_id;
    }

    /**
     * @param $courier_id
     */
    public function setCourierID($courier_id)
    {
        $this->courier_id = (int)$courier_id;
    }

    /**
     * 添加客户记录
     *
     * @param array $crm
     * @return bool
     */
    public function add(array $dict)
    {
        if(empty($dict)) {
            return false;
        }
        return $this->post('v1/courier/crm/add', $this->orzParams($dict));
    }

    /**
     * 删除客户记录
     *
     * @param $id
     * @return bool
     */
    public function del($id)
    {
        if(empty($id) || !is_numeric($id)) {
            return false;
        }
        return $this->post('v1/courier/crm/del', $this->orzParams(['id' => $id]));
    }

    /**
     * 更新客户记录
     *
     * @param $id
     * @param array $crm
     * @return bool
     */
    public function update($id, array $dict)
    {
        if(empty($id) || !is_numeric($id) || empty($dict)) {
            return false;
        }
        return $this->post('v1/courier/crm/update', $this->orzParams(array_merge($dict, ['id' => $id])));
    }

    /**
     * 根据电话号码获取客户信息
     *
     * @param $tel
     * @return bool
     */
    public function getByTel($tel)
    {
        if(empty($tel)) {
            return false;
        }
        return $this->post('v1/courier/crm/getByTel', $this->orzParams(['tel' => (string)$tel]));
    }

    /**
     * 旧版兼容 不要使用
     *
     * @param $tel
     * @return bool
     */
    public function mget($start_timestamp, $is_del = 0, $page_num = 2000)
    {
        $dict = [
            'start_time' => (int)$start_timestamp,
            'is_del' => $is_del ? 1 : 0,
            'page_num' => $page_num
        ];
        return $this->post('v1/courier/crm/mget', $this->orzParams($dict));
    }

    /**
     * @return mixeds
     */
    public function show()
    {
        return $this->post('v1/courier/crm/show', ['courier_id' => $this->getCourierID()]);
    }


    /**
     * 全量同步
     *
     * @param int $page
     * @param int $page_num
     * @return bool
     */
    public function syncAll($page = 1, $page_num = 1000)
    {
        $dict = [
            'page' => (int)$page,
            'page_num' => (int)$page_num
        ];
        return $this->post('v1/courier/crm/syncAll', $this->orzParams($dict));
    }

    /**
     * 增量同步
     *
     * @param $last_sync_time* @param array $change
     * @param string $del_ids
     * @return bool
     */
    public function syncIncr($last_sync_time, array $change = [], $del_ids = '')
    {
        // 如果超过三个月没有同步,则只同步最近三个月更新数据
        $dict = [
            'last_sync_time' => (int)$last_sync_time < strtotime('-3 month') ? strtotime('-3 month') : (int)$last_sync_time,
            'change' => json_encode($change ? $change : []),
            'del_ids' => preg_replace('/\s+/', '', (string)$del_ids)
        ];
        return $this->post('v1/courier/crm/syncIncr', $this->orzParams($dict));
    }

    /**
     * @param array $dict
     * @return bool
     */
    public function addCall($call)
    {
        if(empty($call)) {
            return false;
        }
        return $this->post('v1/courier/crm/addCall', $this->orzParams(['call' => $call]));
    }

    /**
     * 组织请求数据
     *
     * @param array $dict
     * @return array
     */
    protected function orzParams(array $dict)
    {
        return array_merge($dict, ['courier_id' => $this->getCourierID()]);
    }

}